//
//  GifPlistList.m
//  PersonalTool
//
//  Created by Samuel on 2/10/15.
//  Copyright (c) 2015 Samuel. All rights reserved.
//

#import "GifPlistList.h"
#define DocumentsPath [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
#define pathGifImage @"/GifImage"
#define DocumentsPath_pathGifImage [NSString stringWithFormat:@"%@%@",DocumentsPath,pathGifImage]
@implementation GifPlistList
+ (NSString*)saveToPlist:(NSData*)datas
{
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY.MM.dd  HH:mm:ss"];
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    //图片保存的路径
    //这里将图片放在沙盒的documents文件夹中
//    NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
//    NSString *pathGifImage = [NSString stringWithFormat:@"/GifImage"];
//    NSString *DocumentsPath_pathGifImage = [NSString stringWithFormat:@"%@%@",DocumentsPath,pathGifImage];
    //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
    //创建文件夹
    if (![[NSFileManager defaultManager] fileExistsAtPath:DocumentsPath_pathGifImage]) {
        [fileManager createDirectoryAtPath:DocumentsPath_pathGifImage withIntermediateDirectories:YES attributes:nil error:nil];
    }
    //创建文件
    [fileManager createFileAtPath:[NSString stringWithFormat:@"%@/%@",DocumentsPath_pathGifImage,locationString] contents:datas attributes:nil];
//    NSLog(@"%d,%d",fileok,dataok);
    //得到选择后沙盒中图片的完整路径
    NSString *aPath3=[NSString stringWithFormat:@"%@/%@",DocumentsPath_pathGifImage,locationString];
//    NSString *filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,[NSString stringWithFormat:@"/%@.gif",locationString]];
    return aPath3;
}
+ (NSMutableDictionary*)getGifArray;
{
    NSMutableArray *arrayData = [NSMutableArray array];
    NSMutableArray *arrayName = [NSMutableArray array];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
//    NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//    NSString *gifPath = [NSString stringWithFormat:@"%@/GifImage",DocumentsPath];
    NSArray *array = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:DocumentsPath_pathGifImage error:nil];
    for (int i = 0 ; i<array.count; i++) {
        NSString *pathes = [NSString stringWithFormat:@"%@/%@",DocumentsPath_pathGifImage,[array objectAtIndex:i]];
        NSData *gifDatas = [NSData dataWithContentsOfFile:pathes];
        [arrayData insertObject:gifDatas atIndex:0];
        [arrayName insertObject:[array objectAtIndex:i] atIndex:0];
    }
    [dictionary setObject:arrayName forKey:@"Name"];
    [dictionary setObject:arrayData forKey:@"Data"];
    return dictionary;
}
+ (void)removeGifWith:(NSMutableArray*)newArray
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    
    for (int i = 0 ; i < newArray.count; i++) {
        BOOL a = [fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/%@",DocumentsPath_pathGifImage,[newArray objectAtIndex:i]] error:nil];
        if (a) {
            NSLog(@"成功删除 %@",[newArray objectAtIndex:i]);
        }
    }
    
    
}
@end
